﻿using BSDS.Core.Data.Local;
using BSDS.Core.Infrastructure.Logging;
using BSDS.Core.Model.Services;
using BSDS.Core.Model.Conversion;

namespace BSDS.Commands.Commands.Implementation
{
    public class ConvertToXmlCommand : CommandBase
    {
        private readonly string inputFilename;
        private readonly string outputFilename;

        private readonly DataRepository dataRepository;
        private readonly Logger logger;

        public ConvertToXmlCommand(DataRepository dataRepository, Logger logger, string inputFilename, string outputFilename)
        {
            this.dataRepository = dataRepository;
            this.logger = logger;
            this.inputFilename = inputFilename;
            this.outputFilename = outputFilename;
        }

        public override CommandResult Execute(ExecutionContext context)
        {
            logger.Log("Converting input file: " + inputFilename + " to xml file: " + outputFilename);

            var data = dataRepository.LoadFromFile(inputFilename, FormatConvertMode.DontConvert);

            dataRepository.SaveToFile(data, outputFilename, DataFormat.Xml);

            return CommandResult.Success();
        }
    }
}